<?php

$configs = array();

$mysqli = null;

function init_config(){
    global $configs;
    
    $conf_dir = '../conf';
    if(is_dir($conf_dir)) {
        echo "Ok";
    }
    $conf_files = scandir($conf_dir);
    foreach($conf_files as $conf_file){
        $file_info = pathinfo($conf_file);
        if(isset($file_info['extension'])){
            if($file_info['extension'] == "ini"){
                $configs = array_merge($configs,parse_ini_file($conf_dir.'/'.$conf_file,true));
            }
        }

    }
}

function get_config($var, $domain){
    global $configs;
    
    if(empty($configs)){
        init_config();
    }
    
    return (isset($configs[$domain])&&isset($configs[$domain][$var]))?
        $configs[$domain][$var]
        :'';
}

function db_connect(){
    global $mysqli;
    
    $serveur = get_config('host','db');
    $base = get_config('base','db');
    $user = get_config('user','db');
    $pass = get_config('pass','db');

    /*
    $mysqli est une nouvelle instance de la classe mysqli
    prédéfinie dans php et hérite donc de ses propriétés et méthodes
    connexion à la base de données
    */
    $mysqli = new mysqli($serveur, $user, $pass, $base);
    // si la connexion se fait en UTF-8, sinon ne rien indiquer
    $mysqli->set_charset("utf8");
    if ($mysqli->connect_error) {
        print_r('Erreur de connexion ('.$mysqli->connect_errno.')'. $mysqli->connect_error);
    }
}

function db_query($query){
    global $mysqli;
    return mysqli_query($mysqli, $query);
}

function db_numrows($res){
    return mysqli_num_rows($res);
}

function db_fetch_array($res){
    return mysqli_fetch_array($res);
}

function db_fetch_assoc($res){
    return mysqli_fetch_assoc($res);
}

function db_affected_rows(){
    global $mysqli;
    return mysqli_affected_rows($mysqli);
}

function db_last_error(){
    global $mysqli;
    return mysqli_error($mysqli);
}

init_config();
db_connect();